package com.example.ccj.b.c.custom.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RadioButton;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.ccj.R;
import com.example.ccj.b.c.custom.IMRadioButtonListener;
import com.example.ccj.b.c.custom.MRadioButton;
import com.example.ccj.u.StringUtils;
import java.util.Arrays;
import java.util.List;

/**
 * 选项Adapter
 * 含单项和多项
 * 2024年4月27日20:22:29
 */
public class OptionChoiceRecyclerAdapter<T> extends RecyclerView.Adapter<OptionChoiceRecyclerAdapter.ViewHolder2<T>>{
    List<T> optionList;
    IMRadioButtonListener<T> selectListener;
    OptionMode optionMode;
    boolean isReadOnly;

    String[] bianHao = new String[]{"A","B","C","D","E","F","G","H","I","J",};

    /**
     *
     * @param optionList 默认数据String数组
     * @param isReadOnly 选项是否只读
     * @param optionMode 选项模式 单选 or 多选
     * @param selectListener 选择后返回
     */
    public OptionChoiceRecyclerAdapter(List<T> optionList, boolean isReadOnly, OptionMode optionMode, IMRadioButtonListener<T> selectListener){
        this.optionList = optionList;
        this.selectListener = selectListener;
        this.optionMode = optionMode;
        this.isReadOnly = isReadOnly;
        mRadioButtons = new MRadioButton[optionList.size()];
    }

    @NonNull
    @Override
    public ViewHolder2<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder2<>(LayoutInflater.from(parent.getContext()).
                inflate(R.layout.adapter_recycler_option_choice, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder2<T> holder, int position) {
        T t = optionList.get(position);

        if (isReadOnly) {
            holder.mRadioButton.setReadOnly();
        }

        if (optionMode == OptionMode.SINGLE) {
            holder.mRadioButton.setMode(MRadioButton.SINGLE);
        }
        else if (optionMode == OptionMode.MULTIPLE){
            holder.mRadioButton.setMode(MRadioButton.MULTIPLE);
        }

        holder.mRadioButton.setTag(bianHao[position]).setRadioButtonListener(t, selectListener).build();

        mRadioButtons[position] = holder.mRadioButton;
    }


    MRadioButton[] mRadioButtons;

    List<MRadioButton<T>> radioButtonList;


    boolean isCheck;


//    int pos = -1;
//    int oldPos = -1;
//    /**
//     * 初始化单选项控件
//     * @param holder 1
//     * @param position 1
//     */
//    @SuppressLint("SetTextI18n")
//    private void initSingleView(ViewHolder2 holder, int position){
//        String option = optionList.get(position);
//        holder.radioButton.setText(option);
//        holder.radioButton.setId(position);
//        holder.radioButton.setHint(bianHao[position]);
//
//        holder.radioButton.setChecked(position == pos);
//        holder.radioButton.setEnabled(!isReadOnly);
//
//        holder.radioButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
//            if (isChecked) {
//                pos = position;
//                if (pos != oldPos && oldPos > -1) {
//                    notifyItemChanged(oldPos);
//                }
//                oldPos = position;
//                holder.radioButton.setBackgroundResource(R.drawable.yuanjiaolvsebeijing);
//                selectListener.onChange(buttonView.getHint().toString(),option,position);
//            } else {
//                holder.radioButton.setBackgroundResource(R.drawable.yuanjiaowubeijing);
//            }
//        });
//    }

//    String selectValue;
//    /**
//     * 初始化多选项控件
//     * @param holder 1
//     * @param position 1
//     */
//    @SuppressLint("SetTextI18n")
//    private void initMultipleView(ViewHolder2 holder, int position){
//        String option = optionList.get(position);
//        holder.checkBox.setText(bianHao[position] + option);
//        holder.checkBox.setId(position);
//        holder.checkBox.setHint(bianHao[position]);
//        holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
//            if (isChecked) {
//                if (StringUtils.isEmpty(selectValue)) {
//                    selectValue = buttonView.getHint().toString();
//                } else {
//                    selectValue = selectValue + buttonView.getHint().toString();
//                    char[] chars = selectValue.toCharArray();
//                    Arrays.sort(chars);
//                    selectValue = new String(chars);
//                }
//                holder.checkBox.setBackgroundResource(R.drawable.yuanjiaolvsebeijing);
//            } else {
//                if (!StringUtils.isEmpty(selectValue)) {
//                    selectValue = selectValue.replace(buttonView.getHint().toString(),"");
//                }
//                holder.checkBox.setBackgroundResource(R.drawable.yuanjiaowubeijing);
//            }
//            selectListener.onChange(selectValue,option,position);
//        });
//    }

    /**
     * 设置选项是否可用
     * @param isReadOnly 是否只读模式
     */
    public void setOptionChoiceReadOnly(boolean isReadOnly){
        if (this.isReadOnly == isReadOnly) return;
        this.isReadOnly = isReadOnly;
        notifyItemRangeChanged(0,optionList.size()-1);
    }

    @Override
    public int getItemCount() {
        return optionList == null ? 0 : Math.min(optionList.size(), bianHao.length);
    }

    public static class ViewHolder2<T> extends RecyclerView.ViewHolder{
        RadioButton radioButton;
        CheckBox checkBox;
        MRadioButton<T> mRadioButton;
        public ViewHolder2(@NonNull View itemView) {
            super(itemView);
            radioButton = itemView.findViewById(R.id.adapter_recycler_option_choice_rb);
            checkBox = itemView.findViewById(R.id.adapter_recycler_option_choice_cb);
            mRadioButton = itemView.findViewById(R.id.adapter_recycler_option_choice_mrb);
        }
    }
}
